package com.zj.osm.model.dao;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.zj.osm.entity.greendao.Line;
import com.zj.osm.model.pojo.ModuleMaterials;
import com.zj.osm.utils.SQLiteDatabaseUtils;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import static com.zj.utils.utils.MathUtils.eval;

/**
 * @author jiang zhu on 2019/11/3
 */
public class ModuleMaterialsDao {

    private static final String WHERE_VMP_MODULE_ID = "select ModuleMaterialsID,Num,MaterialName,MaterialCodeERP,Spec,Voltage,ERPUnit,TechnicalProtocol from View_ModuleMaterials_ProjectMaterials vmp where vmp.ModuleID = ?";
    private SQLiteDatabase sqLiteDatabase = SQLiteDatabaseUtils.getInstance().getSQLiteDatabase();


    /**
     * 通过moduleId获取物料信息
     *
     * @param moduleID id
     * @return /
     */
    public List<ModuleMaterials> getModuleMaterialsByModuleId(String moduleID) {
        return getModuleMaterialsByModuleId(moduleID, null);
    }

    public List<ModuleMaterials> getModuleMaterialsByModuleId(String moduleID, Line line) {
        List<ModuleMaterials> moduleMaterialsList = new ArrayList<>();
        Cursor cursor = sqLiteDatabase.rawQuery(WHERE_VMP_MODULE_ID, new String[]{moduleID});
        while (cursor.moveToNext()) {
            ModuleMaterials moduleMaterials = new ModuleMaterials();
            moduleMaterials.setMaterialsProjectID(cursor.getString(0));
            moduleMaterials.setMaterialCodeERP(cursor.getString(3));
            moduleMaterials.setMaterialName(cursor.getString(2));
            moduleMaterials.setSpec(cursor.getString(4));
            String erpUnit = cursor.getString(6);
            moduleMaterials.setERPUnit(erpUnit);
            if (line != null) {
                String num = cursor.getString(1);
                //如果是千米或米，保留小数，如果不是，四舍五入取整。
                if ("千米".equals(erpUnit) || "米".equals(erpUnit)) {
                    moduleMaterials.setNum(Double.parseDouble(getNumber(num, Double.parseDouble(line.getLength()) / 1000 + "", line.getBackNum(), line.getNum())) + "");
                } else {
                    moduleMaterials.setNum(Math.round(Double.parseDouble(getNumber(num, Double.parseDouble(line.getLength()) / 1000 + "", line.getBackNum(), line.getNum()))) + "");
                }
            } else {
                moduleMaterials.setNum(cursor.getString(1));
            }
            moduleMaterials.setTechnicalProtocol(cursor.getString(7));
            moduleMaterials.setVoltage(cursor.getString(5));
            moduleMaterialsList.add(moduleMaterials);
        }
        cursor.close();
        return moduleMaterialsList;
    }


    /**
     * 获取数据
     *
     * @param number 数量
     * @param length 长度
     * @param h      回路
     * @param g      根数
     * @return num
     */
    private String getNumber(String number, String length, String h, String g) {
        String replaceResult = (number + "*" + "1").replace("L", length).replace("H", h).replace("G", g);
        NumberFormat nf = NumberFormat.getNumberInstance();
        nf.setMaximumFractionDigits(3);
        return (double) Math.round(eval(replaceResult) * 1000) / 1000 + "";
    }

}
